/*
******************************************************************************************************* 
**  Copyright (C) 2019, 苏州检易生物科技有限公司 
**  All rights reserved. 
** 
**  FileName:       	fetp.c
**  Description:		fpga middle transmit protocol	FPGA与中位机电机协议
**  Author:        	 
**  Version				v0.1
**  Date:           	2019-11-26
**	Function List： 
**  History:         
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                              	调试开关
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                             INCLUDE FILES
*********************************************************************************************************
*/
#include "surface.h"
#include "BspUART.h"
#include "system.h"
/*
*********************************************************************************************************
*                                              	宏定义
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*                                              	结构体定义
*********************************************************************************************************
*/

typedef enum{
	UNINIT   = 'U',
	INITING  = 'I',
	IDLE     = 'N',
	DETECTED = 'D',
	
	ERR 		 = 'E',
}status_t;

typedef enum{
	CMD_INIT = 'I',
	CMD_POLL = 'S',
	CMD_R = 'R',
}surface_cmd_t;

/*
*********************************************************************************************************
*                                              	函数原型声明
*********************************************************************************************************
*/


/*
*********************************************************************************************************
*                                              	全局变量定义
*********************************************************************************************************
*/
extern volatile uint64_t SystemTime;
/*
*********************************************************************************************************
*                                              	模块静态变量定义
*********************************************************************************************************
*/
static status_t status = UNINIT;
static bsp_uart_t* uart_handle;
static uint64_t calib_tick;
static void surface_rx_callback(void* arg,uint8_t *,uint16_t);
/*
*********************************************************************************************************
*                                              	函数定义
*********************************************************************************************************
*/
void surface_bsp_init(void)
{
	uart_handle = bsp_uart_init(USART1,SURFACE_BSP_BAUDRATE,NULL,surface_rx_callback);
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIO_Init(GPIOC,&GPIO_InitStructure);
	
	NVIC_InitTypeDef NVIC_InitStructure;
	EXTI_InitTypeDef  EXTI_InitStructure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC,EXTI_PinSource0);
	
	//EXTI0 NVIC 配置
	NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;//EXTI0中断通道
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;//抢占优先级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;		//子优先级
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQ通道使能
	NVIC_Init(&NVIC_InitStructure);	//根据指定的参数初始化VIC寄存器
	
	EXTI_InitStructure.EXTI_Line=EXTI_Line0;
	EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
	EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising_Falling;
	EXTI_InitStructure.EXTI_LineCmd=ENABLE;
	EXTI_Init(&EXTI_InitStructure);
}
void surface_init(void)
{
	calib_tick = SystemTime;
	bsp_uart_send_byte(uart_handle,CMD_INIT);
	status = INITING;
}
void surface_com(void)
{
	bsp_uart_send_byte(uart_handle,CMD_R);
}
void surface_timer(uint32_t tick)
{
	if(status == INITING && (SystemTime - calib_tick) > 30000)
		status = ERR;
	if(status == INITING && (tick % SURFACE_POLL_INTERVAL == 0))
	{
		bsp_uart_send_byte(uart_handle,CMD_POLL);
	}
}
proto_status_t surface_get_status(void)
{
	switch(status)
	{
		case UNINIT:return P_UNINIT;
		case INITING:return P_INITING;
		case IDLE:return P_IDLE;
		case DETECTED:return P_DETECTED;
		case ERR:return P_ERR;
		default:return P_UNINIT;
	}
	return P_UNINIT;
}
static void surface_rx_callback(void* arg,uint8_t * buff,uint16_t size)
{
	if(size == 1 && buff[0] == IDLE && status == INITING)
	{
		status = IDLE;
	}
	else if(size == 1 && buff[0] == ERR && status == INITING)
	{
		status = ERR;
	}
}
void EXTI0_IRQHandler(void)
{
	if(EXTI_GetITStatus(EXTI_Line0) == 1)
	{
		/*检测到液面时，PC0是低电平*/
		if((GPIOC->IDR & GPIO_Pin_0) == 1)
		{
			if(status == DETECTED)
			{
				status = IDLE;
			}
		}
		else
		{
			if(status == IDLE)
			{
				status = DETECTED;
			}
		}
	}
	EXTI_ClearITPendingBit(EXTI_Line0);
}